<#--package com.han.client.serviceImpl.${module_name};-->
package ${package}serviceImpl;


import ${package}pojo.${pojo_name_query_param};
import ${package}pojo.${pojo_name_vo};
import ${package}pojo.${pojo_name};
import ${package}pojo.PageDTO;
import ${package}pojo.ResultBean;

import ${package}services.${pojo_name_service};
import ${package}mapper.${pojo_name_mapper};
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
/**
* @auth Auto generate
*/
<#if system == "sdds">
@Service
<#else>
@DubboService(group = "${pojo_name_service_impl}", version = "1.0.0", timeout = 15000, retries = 3)
</#if>
public class ${pojo_name_service_impl}  implements ${pojo_name_service}{
    private static final Logger LOG = LoggerFactory.getLogger(${pojo_name_service_impl}.class);

    @Autowired
    private ${pojo_name_mapper} ${pojo_field_name_mapper};

    /**
    * 新增
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public ResultBean insert(${pojo_name_vo} ${pojo_field_name_vo}) {
        try {
            // valid
            if (${pojo_field_name_vo} == null) {
                return ResultBean.error("必要参数缺失");
            }
            LOG.info("${table_remark} 添加的请求参数 "+${pojo_field_name_vo});
            ${pojo_name} ${pojo_name ? uncap_first} = get${pojo_name}(${pojo_field_name_vo});
            ${pojo_field_name_mapper}.insert(${pojo_name ? uncap_first});
            LOG.info("${table_remark} 添加成功");
            return ResultBean.success("添加成功");
        } catch (Exception e) {
            LOG.error("${table_remark} 添加失败 异常原因",e);
        }
        return ResultBean.error("添加失败");

    }
    /**
    * 刪除
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public ResultBean delete(Integer id) {
        try {
            if (id == null) {
                return ResultBean.error("删除的id不能为空");
            }
            LOG.info("${table_remark} 删除的请求参数 "+id);
            int ret = ${pojo_field_name_mapper}.delete(id);
            LOG.info("${table_remark} 删除成功");
            return ResultBean.success("删除成功");
        } catch (Exception e) {
            LOG.error("${table_remark} 删除失败 异常原因",e);
        }
        return ResultBean.error("删除失败");
    }
    /**
    * 批量删除
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public ResultBean batchDelete(List<Integer> ids) {
        try {
            if (ids == null) {
                return ResultBean.error("删除的 id 不能为空");
            }
            LOG.info("${table_remark} 删除的请求参数 "+ids);
            int ret = ${pojo_field_name_mapper}.batchDelete(ids);
            LOG.info("${table_remark} 批量删除成功");
            return ResultBean.success("批量删除成功");
        } catch (Exception e) {
            LOG.error("${table_remark} 批量删除失败 异常原因",e);
        }
        return ResultBean.error("批量删除失败");
    }
    /**
    * 编辑
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public ResultBean update(${pojo_name_vo} ${pojo_field_name_vo}) {
        try {
            // valid
            if (${pojo_field_name_vo} == null) {
                return ResultBean.error("必要参数缺失");
            }
            LOG.info("${table_remark} 编辑的请求参数 "+${pojo_field_name_vo});
            ${pojo_name} ${pojo_name ? uncap_first} = get${pojo_name}(${pojo_field_name_vo});

            int ret = ${pojo_field_name_mapper}.update(${pojo_name ? uncap_first});
            LOG.info("${table_remark} 修改成功");
            return ResultBean.success("修改成功");
        } catch (Exception e) {
            LOG.error("${table_remark} 修改失败 异常原因",e);
        }
        return ResultBean.error("修改失败");

    }

    /**
    * 查询 分页查询
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public PageDTO pageList(${pojo_name_query_param} ${pojo_field_name_query_param}) {
       ` // valid
        if (${pojo_field_name_query_param} == null) {
            return new PageDTO();
        }
        if (Objects.isNull(${pojo_field_name_query_param}.getPage())) {
            ${pojo_field_name_query_param}.setPage(1);
        }
        if (Objects.isNull(${pojo_field_name_query_param}.getLimit())) {
            ${pojo_field_name_query_param}.setLimit(50);
        }
        PageHelper.startPage(${pojo_field_name_query_param}.getPage(), ${pojo_field_name_query_param}.getLimit());
        PageDTO<${pojo_name_vo}> pageDTO = new PageDTO();
        try {
            LOG.info("${table_remark} 查询的请求参数 "+${pojo_field_name_query_param});
            List<${pojo_name}> ${pojo_name ? uncap_first}PageList = ${pojo_name ? uncap_first}Mapper.pageList(${pojo_field_name_query_param});
            PageInfo<${pojo_name}> ${pojo_name ? uncap_first}PageInfo = new PageInfo<>(${pojo_name ? uncap_first}PageList);
            pageDTO.setPage(${pojo_name ? uncap_first}PageInfo.getPageNum());
            pageDTO.setTotal(${pojo_name ? uncap_first}PageInfo.getTotal());
            pageDTO.setLimit(${pojo_field_name_query_param}.getLimit());
            List<${pojo_name}> ${pojo_name ? uncap_first}List = ${pojo_name ? uncap_first}PageInfo.getList();
            List<${pojo_name_vo}> ${pojo_field_name_vo}List = new ArrayList<>();

            for (${pojo_name} ${pojo_name ? uncap_first} : ${pojo_name ? uncap_first}List) {
                ${pojo_name_vo} ${pojo_field_name_vo} = get${pojo_name_vo}(${pojo_name ? uncap_first});
                ${pojo_field_name_vo}List.add(${pojo_field_name_vo});
            }
            pageDTO.setTableData(${pojo_field_name_vo}List);
            LOG.info("${table_remark} 列表查询成功");
            return pageDTO;
        } catch (Exception e) {
            LOG.error("${table_remark} 列表查询失败 异常原因",e);
        }
        pageDTO.setPage(0);
        pageDTO.setTotal(0L);
        pageDTO.setTableData(Collections.EMPTY_LIST);
        return pageDTO;
    }
    /**
    * 详情查询
    * @author Auto generate
    * @date ${gen_time}
    **/
    @Override
    public ResultBean details(Integer id) {
        try {
            if (id == null) {
                return ResultBean.error("查询的id不能为空");
            }
            LOG.info("${table_remark} 查询的请求参数 "+id);
            ${pojo_name} ${pojo_name ? uncap_first} = ${pojo_field_name_mapper}.details(id);
            if (${pojo_name ? uncap_first} == null) {
                LOG.info("${table_remark} 详情查询失败,当前主键的数据在数据库中不存在");
                return ResultBean.error("详情查询失败,当前主键的数据在数据库中不存在");
            }
            LOG.info("${table_remark} 详情查询成功");
            return ResultBean.success("详情查询成功", get${pojo_name_vo}(${pojo_name ? uncap_first}));
        }catch (Exception e){
            LOG.error("${table_remark} 详情查询失败 异常原因",e);
        }
        return ResultBean.error("详情查询失败");
    }

    private ${pojo_name} get${pojo_name}(${pojo_name_vo} ${pojo_field_name_vo}) {
        ${pojo_name} ${pojo_name ? uncap_first} = BeanUtil.copyProperties(${pojo_field_name_vo}, ${pojo_name}.class);
        return ${pojo_name ? uncap_first};
    }

    private ${pojo_name_vo} get${pojo_name_vo}(${pojo_name} ${pojo_name ? uncap_first} ) {
        ${pojo_name_vo} ${pojo_field_name_vo} = BeanUtil.copyProperties(${pojo_name ? uncap_first}, ${pojo_name_vo}.class);
        return ${pojo_field_name_vo};
    }
}
